/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.shared.utils;

import java.awt.*;

/**
 * @author Kristopher T Babic
 */
public class Convert {

    /**
     * I would most likely like to throw some type of exception for these conversions.
     * It could be just a simple as a basic exception.  It doesn't really matter, but it
     * should be better than just to return null or zero.
     */

    public static final String fromPoint(Point in) {

        if (in == null)
            return "\0\0\0\0";


        String retString = "";
        retString += fromInt(in.x);
        retString += fromInt(in.y);

        return retString;
    }

    public static final String fromRectangle(Rectangle in) {

        if (in == null)
            return "\0\0\0\0\0\0\0\0";

        String retString = "";
        retString += fromInt(in.x);
        retString += fromInt(in.y);
        retString += fromInt(in.width);
        retString += fromInt(in.height);

        return retString;
    }

    public static final String fromLong(long in) {

        String binary = Long.toBinaryString(in);

        int diff = 64 - binary.length();
        String retStr = new String();

        if (diff >= 16)
            retStr += (char) 0;
        if (diff >= 32)
            retStr += (char) 0;
        if (diff >= 48)
            retStr += (char) 0;
        if (diff >= 64) {
            retStr += (char) 0;
            return retStr;
        }

        diff = binary.length();  //64 - 16 * (diff / 16);//diff - 16 * (diff / 16);

        int curDiff = diff % 16;

        if (curDiff == 0 && diff != 0)
            curDiff = 16;

        int j = 0;
        while (diff > 0) {
            char tmp = 0;

            for (int i = 0; i < curDiff; i++) { //(16 - curDiff); i++){
                tmp <<= 1;
                if (binary.charAt(j++) == '1')
                    tmp |= 1;////?
            }

            retStr += tmp;

            diff -= curDiff;
            curDiff = diff % 16;
            if (curDiff == 0 && diff != 0)
                curDiff = 16;

        }

        return retStr;
    }

    public static final String fromColor(Color in) {

        if (in == null)
            return "\0\0";

        return fromInt(in.getRGB());

    }

    public static final String fromInt(int in) {

        String binary = Integer.toBinaryString(in);

        int diff = 32 - binary.length();
        String retStr = new String();

        if (diff >= 16)
            retStr += (char) 0;
        if (diff >= 32) {
            retStr += (char) 0;
            return retStr;
        }

        diff = binary.length();//diff - 16 * (diff / 16);

        int curDiff = diff % 16;
        if (curDiff == 0 && diff != 0)
            curDiff = 16;

        int j = 0;
        while (diff > 0) {
            char tmp = 0;

            for (int i = 0; i < (curDiff); i++) {
                tmp <<= 1;
                if (binary.charAt(j++) == '1')
                    tmp |= 1;
            }

            retStr += tmp;

            diff -= curDiff;
            curDiff = diff % 16;
            if (curDiff == 0 && diff != 0)
                curDiff = 16;
        }

        return retStr;

    }

    public static final long toLong(String in) {

        if (in == null || in.length() != 4)
            return (long) 0;

        long retLong = 0;

        retLong |= (int) in.charAt(0);
        retLong <<= 16;

        retLong |= (int) in.charAt(1);
        retLong <<= 16;

        retLong |= (int) in.charAt(2);
        retLong <<= 16;

        retLong |= (int) in.charAt(3);

        return retLong;

    }

    public static final int toInt(String in) {

        if (in == null || in.length() != 2)
            return 0;

        int retInt = 0;

        retInt |= (int) in.charAt(0);
        retInt <<= 16;

        retInt |= (int) in.charAt(1);

        return retInt;

    }

    public static final Color toColor(String in) {

        if (in == null || in.length() != 2)
            return null;

        return new Color(toInt(in), true);

    }

    public static final Point toPoint(String in) {

        if (in == null || in.length() != 4)
            return null;

        return new Point(toInt(in.substring(0, 2)), toInt(in.substring(2, 4)));
    }

    public static final Rectangle toRectangle(String in) {

        if (in == null || in.length() != 8)
            return null;

        return new Rectangle(toInt(in.substring(0, 2)), toInt(in.substring(2, 4)),
                             toInt(in.substring(4, 6)), toInt(in.substring(6, 8)));
    }

}